﻿using System.Text.RegularExpressions;
using System.Web;

namespace Medianamik.Core.Sugar
{
    public static class UrlAmpersandEncoder
    {
        private static readonly Regex _ampersandRegex = new Regex("&(?!#?[a-z0-9]+;)", RegexOptions.Singleline | RegexOptions.Compiled); 
        public static string Encode(string text)
        {
            return !string.IsNullOrEmpty(text) 
                 ? RegularExpressions.Url.Replace(text, Evaluate) 
                 : text;
        }

        private static string Evaluate(Capture url)
        {
            return _ampersandRegex.Replace(url.Value, "&amp;" );
        }
    }
}
